﻿using System.Security.Cryptography;
using System.Text;

namespace PSAMMatrixCloud.Common
{
    /// <summary>
    /// 加密类，提供各种加密算法
    /// </summary>
    public class EncryptHelper
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="bitNum">加密位数，默认32位，支持64位</param>
        /// <param name="srcStr"></param>
        /// <returns></returns>
        public static string MD5Encrypt(int bitNum, string srcStr)
        {
            MD5 md5 = MD5.Create();
            byte[] resultBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(srcStr));

            StringBuilder stringBuilder = new StringBuilder();

            foreach (var result in resultBytes)
            {
                if (bitNum == 64)
                {
                    stringBuilder.Append(result.ToString("x4"));
                }
                else
                {
                    stringBuilder.Append(result.ToString("x2"));
                }
            }

            return stringBuilder.ToString();
        }
    }
}